﻿namespace H2.Streams.Sections
{
    using H2.DataTypes;
    using H2.Streams;
    using System;

    public class Header
    {
        private MapStream Map;

        internal Header(MapStream map)
        {
            this.Map = map;
            this.Map.IndexOffset = this.IndexOffset;
            this.Map.MetaStart = this.MetaStart;
            this.Map.FileTableOffset = this.FileTableOffset;
        }

        public string BuildDate
        {
            get
            {
                return this.Map.ReadStringAt(0x120, 0x20, true, true);
            }
            set
            {
                this.Map.WriteAt(0x120, value.Substring(0, 30), true);
            }
        }

        public int Checksum
        {
            get
            {
                return this.Map.ReadInt32At(720, true);
            }
            set
            {
                this.Map.WriteAt(720, value, true);
            }
        }

        public int Filesize
        {
            get
            {
                return this.Map.ReadInt32At(8, true);
            }
            set
            {
                this.Map.WriteAt(8, value, true);
            }
        }

        public int FileTableCount
        {
            get
            {
                return this.Map.ReadInt32At(0x2c0, true);
            }
            set
            {
                this.Map.WriteAt(0x2c0, value, true);
            }
        }

        public int FileTableIndexOffset
        {
            get
            {
                return this.Map.ReadInt32At(0x2cc, true);
            }
            set
            {
                this.Map.WriteAt(0x2cc, value, true);
            }
        }

        public int FileTableOffset
        {
            get
            {
                return this.Map.ReadInt32At(0x2c4, true);
            }
            set
            {
                this.Map.WriteAt(0x2c4, value, true);
                this.Map.FileTableOffset = value;
            }
        }

        public int FileTableSize
        {
            get
            {
                return this.Map.ReadInt32At(0x2c8, true);
            }
            set
            {
                this.Map.WriteAt(0x2c8, value, true);
            }
        }

        public string Foot
        {
            get
            {
                return this.Map.ReadReverseStringAt(0x7fc, 4, true, true);
            }
            set
            {
                this.Map.WriteReverseStringAt(0x7fc, value.Substring(0, 4), true);
            }
        }

        public string Head
        {
            get
            {
                return this.Map.ReadReverseStringAt(0, 4, false, true);
            }
            set
            {
                this.Map.WriteReverseStringAt(0, value.Substring(0, 4), true);
            }
        }

        public int IndexOffset
        {
            get
            {
                return this.Map.ReadInt32At(0x10, true);
            }
            set
            {
                this.Map.WriteAt(0x10, value, true);
                this.Map.IndexOffset = value;
                this.Map.TagInfoIndexOffset = this.Map.Index.TagInfoIndexOffset;
                this.Map.SecondaryMagic = this.Map.Index.SecondaryMagic;
                this.Map.TagInfoCount = this.Map.Index.TagCount;
            }
        }

        public int IndexSize
        {
            get
            {
                return this.Map.ReadInt32At(20, true);
            }
            set
            {
                this.Map.WriteAt(20, value, true);
            }
        }

        public string InternalName
        {
            get
            {
                return this.Map.ReadStringAt(0x198, 0x20, true, true);
            }
            set
            {
                this.Map.WriteAt(0x15c, value.Substring(0, 0x20), true);
            }
        }

        public H2.DataTypes.MapType MapType
        {
            get
            {
                return (H2.DataTypes.MapType) this.Map.ReadInt32At(320, true);
            }
            set
            {
                this.Map.WriteAt(320, (int) value, true);
            }
        }

        public int MapTypeInt
        {
            get
            {
                return this.Map.ReadInt32At(320, true);
            }
            set
            {
                this.Map.WriteAt(320, value, true);
            }
        }

        public int MetaSize
        {
            get
            {
                return this.Map.ReadInt32At(0x18, true);
            }
            set
            {
                this.Map.WriteAt(0x18, value, true);
            }
        }

        public int MetaStart
        {
            get
            {
                return (this.IndexOffset + this.IndexSize);
            }
        }

        public int NonRawSize
        {
            get
            {
                return this.Map.ReadInt32At(0x1c, true);
            }
            set
            {
                this.Map.WriteAt(0x1c, value, true);
            }
        }

        public string Origin
        {
            get
            {
                return this.Map.ReadStringAt(0x20, 0x100, true, true);
            }
            set
            {
                this.Map.WriteAt(0x20, value.Substring(0, 0x100), true);
            }
        }

        public string ScenarioName
        {
            get
            {
                return this.Map.ReadStringAt(0x1bc, 0x100, true, true);
            }
            set
            {
                this.Map.WriteAt(0x1bc, value.Substring(0, 0x100), true);
            }
        }

        public int StrangeFileStringsOffset
        {
            get
            {
                return this.Map.ReadInt32At(0x158, true);
            }
            set
            {
                this.Map.WriteAt(0x158, value, true);
            }
        }

        public int StrangeFileStringsSize
        {
            get
            {
                return this.Map.ReadInt32At(340, true);
            }
            set
            {
                this.Map.WriteAt(340, value, true);
            }
        }

        public int StringIDCount
        {
            get
            {
                return this.Map.ReadInt32At(0x164, true);
            }
            set
            {
                this.Map.WriteAt(0x164, value, true);
            }
        }

        public int StringIDs128Offset
        {
            get
            {
                return this.Map.ReadInt32At(0x160, true);
            }
            set
            {
                this.Map.WriteAt(0x160, value, true);
            }
        }

        public int StringIDsIndex
        {
            get
            {
                return this.Map.ReadInt32At(0x16c, true);
            }
            set
            {
                this.Map.WriteAt(0x16c, value, true);
            }
        }

        public int StringIDsOffset
        {
            get
            {
                return this.Map.ReadInt32At(0x170, true);
            }
            set
            {
                this.Map.WriteAt(0x170, value, true);
            }
        }

        public int StringIDsSize
        {
            get
            {
                return this.Map.ReadInt32At(360, true);
            }
            set
            {
                this.Map.WriteAt(360, value, true);
            }
        }

        public int Version
        {
            get
            {
                return this.Map.ReadInt32At(4, true);
            }
            set
            {
                this.Map.WriteAt(4, value, true);
            }
        }
    }
}

